package com.example.administrator.view;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import com.example.administrator.adapter.DividerItemDecoration;
import com.example.administrator.adapter.MyAdapter;
import com.example.administrator.adapter.MySystemNameRecycleAdapter;
import com.example.administrator.base.BaseActivity;
import com.example.administrator.bean.EncryptionType;
import com.example.administrator.bean.LoginStatusBean;
import com.example.administrator.bean.SystemNameBean;
import com.example.administrator.module.UrlDefine;
import com.example.administrator.testapk.R;
import com.example.administrator.utils.JsonUtils;
import com.example.administrator.utils.NetUtils;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Response;

public class AtyAddMsg extends BaseActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener{

    @BindView(R.id.et_systemName)
    EditText et_systemName;

    @BindView(R.id.et_account)
    EditText et_account;

    @BindView(R.id.et_pwd)
    EditText et_pwd;

    @BindView(R.id.iv_down)
    ImageView iv_down;

    @BindView(R.id.title_addmsg)
    TitleBar title_addmsg;

    @BindView(R.id.spinner_Encryption)
    Spinner spinner_Encryption;

    @BindView(R.id.btn_addmsg)
    CardView btn_addmsg;

    private static final String TAG = "AtyAddMsg";
    private ArrayList<EncryptionType> mData;
    private ArrayList<SystemNameBean.DataBean> mDatas;
    private MySystemNameRecycleAdapter mMySystemNameRecycleAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private int EncryptionType = 0;
    @Override
    protected int getLayoutResourceId() {
        return R.layout.aty_addmsg;
    }

    //初始化数据
    @Override
    protected void init() {
        NetUtils.getInstance().getDataAsynFromNet(UrlDefine.ACTION_URL_GETSYSTEMALL, new NetUtils.MyNetCall() {
            @Override
            public void success(Call call, Response response) throws IOException {
                final String res = response.body().string();
                Log.e(TAG,res);
                Gson gson = new Gson();
                final SystemNameBean systemNameBean = gson.fromJson(res,SystemNameBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(systemNameBean.getStatus() == 200){
                            mDatas = (ArrayList<SystemNameBean.DataBean>) systemNameBean.getData();
                        }
                    }
                });
            }

            @Override
            public void failed(Call call, IOException e) {
                Log.e(TAG,e.getMessage());
            }
        });
    }

    //初始化布局
    @Override
    protected void initView() {
        iv_down.setOnClickListener(this);
        btn_addmsg.setOnClickListener(this);

        title_addmsg = (TitleBar)findViewById(R.id.title_addmsg);
        title_addmsg.setLeftImageResource(R.mipmap.back);
        title_addmsg.setTitle("添加账号信息");
        title_addmsg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mData = new ArrayList<EncryptionType>();
        mData.add(new EncryptionType(R.mipmap.encryption_normal,"普通"));
        mData.add(new EncryptionType(R.mipmap.encryption_general,"加密"));
        mData.add(new EncryptionType(R.mipmap.encryption_super,"超密"));

        BaseAdapter myAdadpter  = new MyAdapter<EncryptionType>(mData,R.layout.item_spin_hero) {
            @Override
            public void bindView(ViewHolder holder, EncryptionType obj) {
                holder.setImageResource(R.id.img_icon,obj.gethIcon());
                holder.setText(R.id.txt_name, obj.gethName());
            }
        };

        spinner_Encryption.setAdapter(myAdadpter);
        spinner_Encryption.setOnItemSelectedListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_down:
                showPop();
                break;
            case R.id.btn_addmsg:
                if(TextUtils.isEmpty(et_systemName.getText()) ||
                   TextUtils.isEmpty(et_account.getText()) ||
                   TextUtils.isEmpty(et_pwd.getText())){
                    toast("输入项不能留空");
                    return;
                }else {
                    showMyDialog();
                }
                break;
            default:
                break;
        }
    }

    private void showMyDialog() {
        String s;
        if(EncryptionType == 0){
            s = "普通";
        }else if(EncryptionType == 1){
            s = "加密";
        }else{
            s = "超密";
        }

    AlertDialog.Builder buider = new AlertDialog.Builder(AtyAddMsg.this);
        buider.setTitle(R.string.dialog_addmsg_title)
                .setMessage("系统名称:  "+ et_systemName.getText().toString() +
                        "\n账号:  " + et_account.getText().toString() +
                        "\n密码:  " + et_pwd.getText().toString() +
                        "\n加密类型:  " + s)
                .setNegativeButton(R.string.dialog_addmsg_nbtn, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
        }

    });

    buider.setPositiveButton(R.string.dialog_addmsg_pbtn, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {

            final Map<String, String> reqBody = new ConcurrentSkipListMap<>();
            reqBody.put("systemName", et_systemName.getText().toString());
            reqBody.put("account",et_account.getText().toString());
            reqBody.put("password",et_pwd.getText().toString());
            reqBody.put("encryptionLevel", String.valueOf(EncryptionType));

            Log.e(TAG,"systemName" + et_systemName.getText().toString());
            Log.e(TAG,"account" + et_account.getText().toString());
            Log.e(TAG,"password" + et_pwd.getText().toString());
            Log.e(TAG,"encryptionLevel" + String.valueOf(EncryptionType));

            NetUtils.getInstance().postDataAsynToNet(UrlDefine.ACTION_URL_ADDACCOUNT, reqBody, new NetUtils.MyNetCall() {
                @Override
                public void success(Call call, Response response) throws IOException {
                    final String AddMsgResStr = response.body().string();
                    final LoginStatusBean loginStatusBean = JsonUtils.JsonStr2LoginStatusBean(AddMsgResStr);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(loginStatusBean.getMsg().equals("OK")){
                                toast("信息保存成功");
                            }
                        }
                    });
                }

                @Override
                public void failed(Call call, IOException e) {
                    Log.e(TAG,e.getMessage());
                }
            });
        }

    });
    buider.show();
    }

    private void showPop() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.rv_systemname,null);
        //处理popWindow 显示内容
        handleRecyclerView(contentView);
        CustomPopWindow mRvPopWindow= new CustomPopWindow.PopupWindowBuilder(AtyAddMsg.this)
                .setView(contentView)
                .size(700,600)
                .create()
                .showAsDropDown(et_systemName,50,20);
    }

    private void handleRecyclerView(View contentView) {
        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerView);
        mMySystemNameRecycleAdapter = new MySystemNameRecycleAdapter(AtyAddMsg.this, mDatas);
        mLayoutManager = new LinearLayoutManager(AtyAddMsg.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setAdapter(mMySystemNameRecycleAdapter);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setItemAnimator(new DefaultItemAnimator());//默认动画
        recyclerView.setHasFixedSize(true);//效率最高

        mMySystemNameRecycleAdapter.setOnItemClickListener(new MySystemNameRecycleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                et_systemName.setText(mDatas.get(position).getSystemName());
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.spinner_Encryption:
                EncryptionType = position;
                break;
            default:
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}